package com.yungam.imcs.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yungam.common.bean.energy.Energy;
import com.yungam.common.bean.equip.Equipment;
import com.yungam.common.service.EnergyService;
import com.yungam.common.service.EquipmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class EnergyJob {

    @Resource
    private EnergyService energyService;
    @Resource
    private EquipmentService equipmentService;

    /**
     * 一小时执行一次
     */
    @Scheduled(fixedRate = 1000 * 60 * 60)
    public void check() {
        // 先检查今日的数据有没有生成, 生成了则每小时更新, 还未生成则插入
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String str = now.format(df);
        String start = str + " 00:00:00", end = str + " 23:59:59";
        List<Energy> exist = energyService.list(new QueryWrapper<Energy>().between("day", start, end));
        if (exist.size() == 0) { // 插入
            // 获取昨日累计值, 没有则为0
            List<Equipment> equipmentList = equipmentService.list(new QueryWrapper<Equipment>()
                    .select(Equipment.class, col -> !col.getProperty().equals("equImg")));
            LocalDateTime lastDay = now.minusDays(1);
            String lastStart = lastDay.format(df) + " 00:00:00", lastEnd = lastDay.format(df) + " 23:59:59";
            List<Energy> lastList = energyService.list(new QueryWrapper<Energy>().between("day", lastStart, lastEnd));
            Map<Integer, Double> map = Maps.newHashMap();
            if (lastList.size() != 0) {
                for (Energy energy : lastList) map.put(energy.getEquId(), energy.getNewValue());
            }
            List<Energy> add = Lists.newArrayList();
            for (Equipment equipment : equipmentList) {
                Energy energy = new Energy();
                DecimalFormat format = new DecimalFormat("#.##");
                double newVal = Double.parseDouble(format.format(Math.random() * 100));
                energy.setEquId(equipment.getEquId())
                        .setDay(new Date())
                        .setOldValue(map.getOrDefault(equipment.getEquId(), 0d))
                        .setNewValue(newVal);
                add.add(energy);
            }
            // save
            energyService.saveBatch(add);
        } else { // 更新
            List<Energy> update = Lists.newArrayList();
            for (Energy energy : exist) {
                DecimalFormat format = new DecimalFormat("#.##");
                Double newVal = energy.getNewValue();
                if (newVal == null) newVal = 0d;
                double random = Double.parseDouble(format.format(Math.random() * 100 + newVal));
                energy.setNewValue(random);
                update.add(energy);
            }
            energyService.updateBatchById(update);
        }
    }
}
